/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.otter.node.common.communication;

import com.alibaba.otter.node.common.config.ConfigClientService;
import com.alibaba.otter.shared.communication.core.CommunicationEndpoint;
import com.alibaba.otter.shared.communication.core.impl.dubbo.DubboCommunicationEndpoint;
//import com.alibaba.otter.shared.communication.core.impl.rmi.RmiCommunicationEndpoint;
import lombok.Setter;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * 基于Node节点的endpoint包装
 *
 * @author jianghang 2011-10-18 下午02:28:25
 * @version 4.0.0
 */
@Setter
public class NodeCommunicationEndpoint implements InitializingBean, DisposableBean {

    private CommunicationEndpoint endpoint;
    private ConfigClientService configClientService;

    public void afterPropertiesSet() throws Exception {

        Long port = configClientService.currentNode().getPort();
//        if (endpoint instanceof RmiCommunicationEndpoint rmiEndpoint) {
//            String ip = config.currentNode().getIp();
//            rmiEndpoint.setHost(ip);
//            rmiEndpoint.setPort(port.intValue());
//        }

        if (endpoint instanceof DubboCommunicationEndpoint dubboEndpoint) {
            dubboEndpoint.setPort(port.intValue());
        }
        endpoint.initial();
    }

    public void destroy() throws Exception {
        endpoint.destroy();
    }

}
